package com.ecable.action;

import java.io.IOException;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.ecable.exception.ConnectionException;
import com.ecable.delegate.StateMgrDelegate;

import com.ecable.util.UtilConstants;

public class CancelStateAction extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8416347923246299062L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		 String path="";
			
		boolean flag=false;
			
			try{
				
				try{
					
					int sid=Integer.parseInt(request.getParameter("sid"));
					
				flag=new StateMgrDelegate().deleteState(sid);
					
				}
				catch (ConnectionException e) {
			   throw new ServletException("ConnectionTO Failed");
			   
				}
				
				
				if(flag){
					
					request.setAttribute("status", UtilConstants._CANCEL_STATE);
					
			
					
					path=UtilConstants._DISPLAY_STATE_HOME_ACTION;
					
				}
				else{
					
					request.setAttribute("status", UtilConstants._CANCEL_STATE_FAILED);
					
					path=UtilConstants._DISPLAY_STATE_HOME_ACTION;
					
				}
				
				
		}
			catch (Exception e) {
				e.printStackTrace();
				
				request.setAttribute("status", UtilConstants._INVALIED_ENTRY);
				
				path=UtilConstants._DISPLAY_STATE_HOME_ACTION;
				
			}
			
			
			RequestDispatcher rd=request.getRequestDispatcher(path);
			
			rd.forward(request, response);
			
		
		
		
		
		
	}

}
